package com.baomidou.springwind.excel;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.baomidou.springwind.entity.Resource;
import com.baomidou.springwind.enums.ResourceEnum;
import com.baomidou.springwind.service.ResourceService;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * excel文件读取
 *
 * @author luox
 */
@Component
public class ResourceDataReader extends ExcelReader<Resource> {

    @Autowired
    private ResourceService resourceService;

    @Override
    protected String saveData(List<Resource> dataList, Long batchId) {
        EntityWrapper<Resource> wrapper = new EntityWrapper<Resource>();
        wrapper.eq("batch_id", batchId);
        resourceService.delete(wrapper);//删除之前导入的数据
        for (Resource resource : dataList) {
            resource.setBatchId(batchId);
            resource.setCreateTime(new Date());
            resource.setOperatorId(getCurrentUserId().intValue());
        }
        resourceService.insertBatch(dataList, 100);
        return "";
    }

    @Override
    protected List<Resource> verifyData(HSSFWorkbook workbook) {
        List<Resource> resourceList = new ArrayList<Resource>(ResourceEnum.values().length);
        for (ResourceEnum resourceEnum : ResourceEnum.values()) {
            String type = String.valueOf(resourceEnum.getType());
            if (type.startsWith("1")) {
                resourceList.add(buildOperator(resourceEnum, workbook));
            } else if (type.startsWith("2")) {
                resourceList.add(buildEquipment(resourceEnum, workbook));
            } else if (type.startsWith("3")) {
                resourceList.add(buildWorkSpace(resourceEnum, workbook));
            }
        }
        return resourceList;
    }

    @Override
    protected List<Resource> verifyData7(XSSFWorkbook workbook) {
        return null;
    }

    private Resource buildOperator(ResourceEnum resEnum, HSSFWorkbook workbook) {
        Resource resource = new Resource();
        HSSFSheet sheet = workbook.getSheetAt(0);
        resource.setResType(resEnum.getType());
        String amount = getStringCellValue(sheet.getRow(3), resEnum.getCellIndex());
        String ability = getStringCellValue(sheet.getRow(4), resEnum.getCellIndex());
        String locomotivity = getStringCellValue(sheet.getRow(5), resEnum.getCellIndex());
        String price = getStringCellValue(sheet.getRow(6), resEnum.getCellIndex());
        if (StringUtils.isNotEmpty(amount)) {
            resource.setAmount(new BigDecimal(amount));
        }
        if (StringUtils.isNotEmpty(ability)) {
            resource.setAbility(Integer.parseInt(ability));
        }
        if (StringUtils.isNotEmpty(locomotivity)) {
            resource.setLocomotivity(Integer.parseInt(locomotivity));
        }
        if (StringUtils.isNotEmpty(price)) {
            resource.setPrice(new BigDecimal(price));
        }
        return resource;
    }

    private Resource buildEquipment(ResourceEnum resEnum, HSSFWorkbook workbook) {
        Resource resource = new Resource();
        HSSFSheet sheet = workbook.getSheetAt(0);
        resource.setResType(resEnum.getType());
        String amount = getStringCellValue(sheet.getRow(10), resEnum.getCellIndex());
        String ability = getStringCellValue(sheet.getRow(11), resEnum.getCellIndex());
        String locomotivity = getStringCellValue(sheet.getRow(12), resEnum.getCellIndex());
        String price = getStringCellValue(sheet.getRow(13), resEnum.getCellIndex());
        if (StringUtils.isNotEmpty(amount)) {
            resource.setAmount(new BigDecimal(amount));
        }
        if (StringUtils.isNotEmpty(ability)) {
            resource.setAbility(Integer.parseInt(ability));
        }
        if (StringUtils.isNotEmpty(locomotivity)) {
            resource.setLocomotivity(Integer.parseInt(locomotivity));
        }
        if (StringUtils.isNotEmpty(price)) {
            resource.setPrice(new BigDecimal(price));
        }
        return resource;
    }

    private Resource buildWorkSpace(ResourceEnum resEnum, HSSFWorkbook workbook) {
        Resource resource = new Resource();
        HSSFSheet sheet = workbook.getSheetAt(0);
        resource.setResType(resEnum.getType());
        String amount = getStringCellValue(sheet.getRow(17), resEnum.getCellIndex());
        String price = getStringCellValue(sheet.getRow(18), resEnum.getCellIndex());
        if (StringUtils.isNotEmpty(amount)) {
            resource.setAmount(new BigDecimal(amount));
        }
        if (StringUtils.isNotEmpty(price)) {
            resource.setPrice(new BigDecimal(price));
        }
        return resource;
    }
}
